// 参考文档: https://www.electronjs.org/zh/docs/latest/api/dialog#dialogshowerrorboxtitle-content

const { BrowserWindow, dialog } = require('electron');
class DialogManager {
  /**
   * 通用对话框封装
   * @param {Object} options 对话框配置
   * @param {BrowserWindow} [window] 关联窗口（用于模态对话框）
   */
  static async showMessage(options, window) {
    const defaultOptions = {
      title: '提示',
      buttons: ['取消', '确定'],
      defaultId: 1,
      cancelId: 0,
      noLink: true,
      ...options
    };

    try {
      const { response, checkboxChecked } = await dialog.showMessageBox(
        window || null,
        defaultOptions
      );
      return { response, checkboxChecked };
    } catch (error) {
      console.error('Dialog error:', error);
      return null;
    }
  }

  /**
   * 文件选择对话框
   * @param {Object} options 文件对话框配置
   */
  static async openFileDialog(options = {}) {
    const defaultOptions = {
      title: '选择文件',
      properties: ['openFile', 'openDirectory'],
      filters: [
        { name: 'All Files', extensions: ['*'] }
      ]
    };

    const { canceled, filePaths } = await dialog.showOpenDialog({
      ...defaultOptions,
      ...options
    });

    return canceled ? null : filePaths;
  }

  /**
   * 错误提示
   * @param {string} title 标题
   * @param {string} content 错误内容
   */
  static showErrorDialog(title, content) {
    try {
      dialog.showErrorBox(title || '错误', content);
    } catch (error) {
      console.error('Error dialog failed:', error);
    }
  }

  /**
   * 文件保存对话框
   * @param {Object} options 文件保存配置
   */
  static async showSaveDialog(options = {}) {
    const defaultOptions = {
      title: '保存',
      properties: ['showOverwriteConfirmation'],
      filters: [
        { name: 'All Files', extensions: ['*'] }
      ]
    };
    const { canceled, filePaths } = await dialog.showSaveDialog({
      ...defaultOptions,
      ...options
    });

    return canceled ? null : filePaths;
  }
}

// 导出单例
module.exports = DialogManager;
